// 加载分组中的股票
function loadGroupStocks(groupId) {
    $.ajax({
        url: `/stockGroup/stocks/${groupId}`,
        method: 'GET',
        success: function (res) {
            if (res.code === "0000") {
                displayGroupStocks(groupId, res.data);
            }
        }
    });
}

// 显示分组中的股票
function displayGroupStocks(groupId, stocks) {
    const tbody = $('#groupStocksTable');
    tbody.empty();

    if (stocks.length === 0) {
        tbody.append('<tr><td colspan="5" class="text-center text-muted">该分组暂无股票</td></tr>');
        return;
    }

    stocks.forEach(stock => {
        const changeClass = stock.changePercent >= 0 ? 'positive' : 'negative';
        const changeSymbol = stock.changePercent >= 0 ? '+' : '';

        // 检查是否需要显示星号
        const displayName = stock.monitorFlag === 1 ? `${stock.name} <span style="color: red;">★</span>` : stock.name;

        const row = $(`
            <tr>
                <td>${stock.code}</td>
                <td>${displayName}</td>
                <td>${stock.price}</td>
                <td class="${changeClass}">${changeSymbol}${stock.changePercent}</td>
                <td>
                    <button class="btn btn-sm btn-outline-primary view-detail-btn me-1" 
                            data-stock-code="${stock.code}" 
                            data-stock-name="${stock.name}">详情</button>
                    <button class="btn btn-sm btn-outline-warning move-stock-btn me-1" 
                            data-stock-code="${stock.code}"
                            data-stock-name="${stock.name}">移动</button>
                    <button class="btn btn-sm btn-outline-danger remove-stock-btn" 
                            data-stock-code="${stock.code}"
                            data-stock-name="${stock.name}">移除</button>
                    <button class="btn btn-sm btn-outline-primary view-pushTop-btn me-1"
                        data-stock-code="${stock.code}"
                        data-stock-groupid="${groupId}">置顶</button>
                    <button class="btn btn-sm btn-outline-primary view-top-btn me-1"
                        data-stock-code="${stock.code}"
                        data-stock-groupid="${groupId}">固顶</button>
                    <button class="btn btn-sm btn-outline-success add-monitor-btn me-1"
                        data-stock-code="${stock.code}"
                        data-stock-name="${stock.name}" data-stock-groupid="${groupId}">监控</button>
                </td>
            </tr>
        `);

        tbody.append(row);
    });

    // 绑定查看详情按钮事件
    $('.view-detail-btn').click(function () {
        const stockCode = $(this).data('stock-code');
        const stockName = $(this).data('stock-name');
        showStockDetail(stockCode, stockName);
    });

    // 绑定移动股票按钮事件
    $('.move-stock-btn').click(function () {
        const stockCode = $(this).data('stock-code');
        const stockName = $(this).data('stock-name');
        showMoveStockDialog(stockCode, stockName);
    });

    // 绑定移除股票按钮事件
    $('.remove-stock-btn').click(function () {
        const stockCode = $(this).data('stock-code');
        const stockName = $(this).data('stock-name');

        if (confirm(`确定要将股票 "${stockName}" 从当前分组中移除吗？`)) {
            removeStockFromGroup(stockCode, currentGroupId);
        }
    });
    // 置顶
    $('.view-pushTop-btn').click(function () {
        const stockCode = $(this).data('stock-code');
        const groupId = $(this).data('stock-groupid');

        if (confirm(`确定要将股票置顶吗？`)) {
            pushStockTop(stockCode, groupId);
        }
    });
    // 固顶
    $('.view-top-btn').click(function () {
        const stockCode = $(this).data('stock-code');
        const groupId = $(this).data('stock-groupid');

        if (confirm(`确定要将股票固顶吗？`)) {
            fixedStockTop(stockCode, groupId);
        }
    });
    // 监控
    $('.add-monitor-btn').click(function () {
        const stockCode = $(this).data('stock-code');
        const stockName = $(this).data('stock-name');
        const groupId = $(this).data('stock-groupid');

        $.ajax({
            url: `/stockMonitor/detail/${stockCode}`,
            method: 'GET',
            success: function (res) {
                if (res.code === "0000") {
                    const stock = res.data;
                    showMonitorDialog(groupId, stockCode, stockName, stock.costPrice, stock.monitorPrice);
                }
            }
        });

    });
}